/*  GStruct: data structure helpers for Java
 *  Copyright (C) 2011 Giulio Franco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package gstruct.cli;

import java.util.Iterator;

/**
 * Generic command-line argument parser
 * @author Giulio Franco <giulio_franco@msn.com>
 *
 * @param <ResultType>
 */
public class CLIArgument<ResultType> {
	public static enum Errors {
		DUPLICATE("cliarg.error.duplicate");
		
		public final String errorFormat;
		private Errors(String errorFormat) {
			this.errorFormat = errorFormat;
		}
	}
	
	private final String name;
	private final String description;
	private final boolean unique;
	private final boolean required;
	
	/**
	 * Creates a non-required non-unique CLIArgument
	 * @param name			Name of the argument (the one users write to use this argument)
	 * @param description	Human-readable description of the argument
	 */
	public CLIArgument(String name, String description) {
		this(name, description, false, false);
	}
	
	/**
	 * Creates a non-unique CLIArgument
	 * @param name			Name of the argument (the one users write to use this argument)
	 * @param description	Human-readable description of the argument
	 * @param required		Boolean indicating if the argument is required
	 */
	public CLIArgument(String name, String description, boolean required) {
		this(name, description, required, false);
	}
	
	/**
	 * Creates a CLIArgument
	 * @param name			Name of the argument (the one users write to use this argument)
	 * @param description	Human-readable description of the argument
	 * @param required		Boolean indicating if the argument is required
	 * @param unique		Boolean indicating if the argument can be specified at most once
	 */
	public CLIArgument(String name, String description, boolean required, boolean unique) {
		this.name = name;
		this.description = description;
		this.unique = unique;
		this.required = required;
	}
	
	/**
	 * Gets the name of this argument
	 * @return	Name of the argument
	 */
	public String matcher() {
		return name;
	}
	
	/**
	 * Called upon insertion of the argument
	 * @param matcher	String used to call the argument
	 * @param args		Iterator of the following arguments
	 * @param result	Result of the argument parse
	 * @return			True iff the parsing can continue
	 */
	public boolean inserted(String matcher,
			Iterator<String> args, ParseResult<? extends ResultType> result) {
		
		if (isUnique() && result.getArguments().contains(this)) {
			result.addError(new ParseError<ResultType>(
					this, Errors.DUPLICATE.errorFormat, matcher()));
		}
		return true;
	}
	
	/**
	 * Called upon parse completion
	 * @param result	Result of the argument parse
	 * @return			True iff the parsing can continue
	 */
	public boolean isOk(ParseResult<? extends ResultType> result) {
		return true;
	}

	/**
	 * Gets the name of this argument
	 * @return	Name of the argument
	 */
	public String getName() {
		return name;
	}

	/**
	 * Gets the description of this argument
	 * @return	Description of the argument
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Checks if this argument is unique (can be used at most once)
	 * @return	True iff the argument is unique
	 */
	public boolean isUnique() {
		return unique;
	}

	/**
	 * Checks if this argument is required (must be used at least once)
	 * @return	True iff the argument is required
	 */
	public boolean isRequired() {
		return required;
	}
}
